package com.unisv.dj;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.unisv.dj.R.id;
import com.unisv.dj.util.Command;
import com.unisv.dj.util.InitParameter;

/**
 * <p>
 * init activity
 * </p>
 * 
 * @author fanzhen
 * @time 8:04:13 PM Sep 23, 2011
 * @email fanzhen.meng@gmail.com
 */
public class AutoActivity extends Activity {
	private static final String TAG = "AutoActivity";
	BroadcastReceiver commandReceiver;
	IntentFilter movementFilter;
	String currentP = "P1";
	boolean timerFlag = false;
	int currentSpeed;
	private CountDownTimer countDownTimer;
	private ImageView autoPlus;
	private ImageView autoStart;
	private ImageView autoStop;
	private ImageView autoShort;
	private ImageView autoMusic;
	private ImageView autoVideo;
	private TextView tvautorun;
	private TextView autocountdown;
	private TextView autospeed;

	void initStart() {
		timerFlag = false;
		countDownTimer.cancel();
		currentSpeed = InitParameter.getP1(currentP, 0);
		tvautorun.setText(String.format("Auto:%1s", currentP));
		autospeed.setText("Speed:"
				+ String.valueOf(InitParameter.getP1(currentP, 0)));
		autocountdown.setText("Count:10/0");
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.auto);
		autoPlus = (ImageView) this.findViewById(R.id.autoPlus);
		autoStart = (ImageView) this.findViewById(R.id.autoStart);
		autoStop = (ImageView) this.findViewById(R.id.autoStop);
		autoShort = (ImageView) this.findViewById(R.id.autoShort);
		autoMusic = (ImageView) this.findViewById(R.id.autoMusic);
		autoVideo = (ImageView) this.findViewById(R.id.autoVideo);
		tvautorun= (TextView) this.findViewById(R.id.tvautorun);
		autocountdown = (TextView) this.findViewById(R.id.autocountdownid);
		autospeed = (TextView) this.findViewById(id.autospeedid);

		autoPlus.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// P+
				if (currentP.equals("P1")) {
					currentP = "P2";
				} else if (currentP.equals("P2")) {
					currentP = "P3";
				} else if (currentP.equals("P3")) {
					currentP = "P4";
				} else if (currentP.equals("P4")) {
					currentP = "P5";
				}
				initStart();
			}
		});

		autoStart.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Start
				if (!timerFlag) {
					timerFlag = true;
					countDownTimer.start();
					// Command.start();
					Command.changeSpeed(currentSpeed, currentSpeed);
				}
			}
		});

		autoStop.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Stop
				initStart();
				timerFlag = false;
				// Command.stopShort();
				Command.changeSpeed(0, 0);
			}
		});

		autoStop.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View arg0) {
				// TODO Auto-generated method stub
				initStart();
				timerFlag = false;
				// Command.stopLong();
				Command.changeSpeed(0, 0);
				finish();
				return false;
			}
		});

		autoShort.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// P-
				if (currentP.equals("P5")) {
					currentP = "P4";
				} else if (currentP.equals("P4")) {
					currentP = "P3";
				} else if (currentP.equals("P3")) {
					currentP = "P2";
				} else if (currentP.equals("P2")) {
					currentP = "P1";
				}
				initStart();
			}
		});

		autoMusic.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Music
				Intent intent = new Intent(
						MediaStore.INTENT_ACTION_MUSIC_PLAYER);
				startActivity(intent);
			}
		});

		autoVideo.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// Vidio
				Intent intent = new Intent();  
				intent.setAction(android.content.Intent.ACTION_VIEW);
				intent.setDataAndType(Uri.EMPTY, "video/*");
				startActivity(intent);
			}
		});

		countDownTimer = new CountDownTimer(10 * 60 * 1000L, 1000L) {
			@Override
			public void onTick(long millisUntilFinished) {
				int currentMinute = (int) millisUntilFinished / 1000 / 60;
				autocountdown.setText(String.format("Count:%1$d/%2$d",
						currentMinute, millisUntilFinished / 1000 % 60));
				if (millisUntilFinished / 1000 % 60 == 0) {
					Log.i(TAG, String.valueOf(currentMinute));
					currentSpeed = InitParameter.getP1(currentP,
							10 - currentMinute);
					autospeed.setText(String.valueOf(currentSpeed));
					Command.changeSpeed(currentSpeed, currentSpeed);
					autospeed.setText("Speed:"
							+ String.valueOf(InitParameter.getP1(currentP,
									10 - currentMinute)));
				}
			}

			@Override
			public void onFinish() {
				autocountdown.setText("DONE!");
				autospeed.setText("Speed:"
						+ String.valueOf(InitParameter.getP1(currentP, 0)));
				timerFlag = false;
				// Command.stopShort();
				Command.changeSpeed(0, 0);
			}
		};

		initStart();
	}

	@Override
	public void onResume() {
		if (movementFilter == null)
			movementFilter = new IntentFilter(ServerService.COMMAND_COMING);
		if (commandReceiver == null)
			commandReceiver = new BroadcastReceiver() {
				@Override
				public void onReceive(Context context, Intent intent) {
					// Log.i(TAG, "msg from service.............." + command);
					byte[] comm = intent
							.getByteArrayExtra(ServerService.COMMAND_TAG);
					if (comm.length == 4 && comm[0] == 111) {
						switch (comm[3]) {
						case 1:
							autoStart.performClick();
							break;
						case 2:
							autoStop.performClick();
							break;
						case 3:
							autoStop.performLongClick();
							break;
						case 4:
							autoPlus.performClick();
							break;
						case 5:
							autoShort.performClick();
							break;
						case 6:
							autoPlus.performClick();
							break;
						case 7:
							autoShort.performClick();
							break;

						default:
							break;
						}
					}
				}
			};
		registerReceiver(commandReceiver, movementFilter);
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (commandReceiver != null) {
			unregisterReceiver(commandReceiver);
			commandReceiver = null;
		}
		if (movementFilter != null)
			movementFilter = null;
		super.onDestroy();
	}
}